package com.zuxia.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.zuxia.dao.IDepartDao;
import com.zuxia.entity.Depart;

@Repository("departDao")
public class DepartDao  implements IDepartDao{
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	public List<Depart> findAll() {
		return (List<Depart>) hibernateTemplate.find("FROM Depart");
	}
	public String saveDepart(Depart depart) {

		try {
			hibernateTemplate.save(depart);
			return "yes";
		} catch (Exception e) {
			return "no";
		}
	}
	public String updateDepart(Depart depart) {
		
		SessionFactory factory = hibernateTemplate.getSessionFactory();
		Session session = factory.getCurrentSession();
		Depart depart2 = session.get(Depart.class,depart.getDepartId());
		depart2.setDepartName(depart.getDepartName());
		depart2.setDepartRemark(depart.getDepartRemark());
		try {
			session.saveOrUpdate(depart2);
			return "yes";
		} catch (Exception e) {
			return "no";
		}
	}
	public String deleteDepart(Depart depart) {

		SessionFactory factory = hibernateTemplate.getSessionFactory();
		Session session = factory.getCurrentSession();
		Depart depart2 = session.get(Depart.class,depart.getDepartId());
		depart2.setDepartName(depart2.getDepartName());
		depart2.setDepartRemark(depart2.getDepartRemark());
		try {
			session.delete(depart2);
			return "yes";
		} catch (Exception e) {
			return "no";
		}
		
	}

}
